// 文件路径: com.huaxin.device.service.impl.DeviceReadingCircuitBreakerServiceImpl.java
package com.huaxin.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaxin.device.dto.ResponseCode;
import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.request.DeviceReadingCircuitBreakerCreateReqDTO;
import com.huaxin.device.dto.request.DeviceReadingCircuitBreakerPageListReqDTO;
import com.huaxin.device.mapper.DeviceReadingCircuitBreakerMapper;
import com.huaxin.device.mapping.DeviceReadingCircuitBreakerMapping;
import com.huaxin.device.models.DeviceReadingCircuitBreaker;
import com.huaxin.device.service.DeviceReadingCircuitBreakerService;
import lombok.RequiredArgsConstructor;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.stereotype.Service;

import static org.springframework.util.StringUtils.hasText;

@Service
@RequiredArgsConstructor
public class DeviceReadingCircuitBreakerServiceImpl extends ServiceImpl<DeviceReadingCircuitBreakerMapper, DeviceReadingCircuitBreaker> implements DeviceReadingCircuitBreakerService {

    private final DeviceReadingCircuitBreakerMapping mapping;

    @Override
    public ResponseResult<DeviceReadingCircuitBreaker> create(DeviceReadingCircuitBreakerCreateReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数不能为空");
        }
        DeviceReadingCircuitBreaker entity = mapping.toEntity(in);
        if (!this.save(entity)) {
            return ResponseResult.failure(ResponseCode.INTERNAL_SERVER_ERROR, "添加失败，请稍后再试！", null);
        }
        DeviceReadingCircuitBreaker created = this.getById(entity.getId());
        return ResponseResult.ok(created);
    }

    @Override
    public DeviceReadingCircuitBreaker getLatestByDeviceId(String deviceId) {
        if (StringUtils.isBlank(deviceId)) {
            throw new IllegalArgumentException("设备ID不能为空");
        }

        LambdaQueryWrapper<DeviceReadingCircuitBreaker> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeviceReadingCircuitBreaker::getDeviceId, deviceId)
                .orderByDesc(DeviceReadingCircuitBreaker::getCreatedAt)
                .last("LIMIT 1"); // 获取最新一条

        return this.getOne(wrapper);
    }

    @Override
    public ResponseResult<IPage<DeviceReadingCircuitBreaker>> pageQuery(DeviceReadingCircuitBreakerPageListReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数不能为空");
        }

        // 分页对象
        IPage<DeviceReadingCircuitBreaker> page = new Page<>(in.getPageNum(), in.getPageSize());

        // 查询条件
        LambdaQueryWrapper<DeviceReadingCircuitBreaker> wrapper = new LambdaQueryWrapper<>();

        // 按设备ID过滤
        if (hasText(in.getDeviceId())) {
            wrapper.eq(DeviceReadingCircuitBreaker::getDeviceId, in.getDeviceId());
        }

        // 按时间范围过滤
        if (in.getStartTime() != null) {
            wrapper.ge(DeviceReadingCircuitBreaker::getCreatedAt, in.getStartTime());
        }
        if (in.getEndTime() != null) {
            wrapper.le(DeviceReadingCircuitBreaker::getCreatedAt, in.getEndTime());
        }

        // 按读数时间倒序
        wrapper.orderByDesc(DeviceReadingCircuitBreaker::getCreatedAt);

        IPage<DeviceReadingCircuitBreaker> resultPage = this.page(page, wrapper);
        return ResponseResult.ok(resultPage);
    }
}